import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class MainH2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] arrB = new int[n]; //B每天的等级
        for (int i = 0; i < n; i++) {
            arrB[i] = sc.nextInt();
        }

        boolean[] flag = new boolean[n + 1];

        //key为B的等级，value为该等级对应的价值
        HashMap<Integer, Integer> mapB = new HashMap<>();
        for (int i = 0; i < n; i++) {
            mapB.put(arrB[i], i + 1);
        }

        Arrays.sort(arrB);

        int ret = 0;
        int minA = 1;
        for (int i = 0; i < n; i++) {
            int numB = arrB[i]; //B的等级
            if (numB + 1 < n + 1 && !flag[numB + 1]) { //比它大的还没用过
                ret += mapB.get(numB);
                flag[numB + 1] = true;
            } else if ((numB + 1 < n + 1 && flag[numB + 1]) || numB + 1 >= n + 1) { //比他大的用过了，拿A的最小值与之匹配
                flag[minA] = true;
                if (minA != numB) {
                    ret -= mapB.get(numB);
                }
                minA++;
            }
        }

        System.out.println(ret);

        sc.close();
    }
}
